package com.haixiaoke.saas.enterprise.lock.controller;

import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.lock.domain.LockPwd;
import com.haixiaoke.saas.lock.service.ILockPwdService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.haixiaoke.saas.common.tenant.TenantContext.getTenantId;


/**
 * 锁密码信息Controller
 *
 * @author qixi
 * @date 2024-07-29
 */
@RestController
@RequestMapping("/enterprise/lock/lockPwd")
public class EnterpriseLockPwdController extends BaseController {
    @Autowired
    private ILockPwdService lockPwdService;

    /**
     * 查询锁密码信息列表
     */
    // @PreAuthorize("@ss.hasPermi('lock:lockPwd:list')")
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(LockPwd lockPwd) {
        startPage();
        lockPwd.setTenantId(getTenantId());
        List<LockPwd> list = lockPwdService.selectLockPwdList(lockPwd);
        return getDataTable(list);
    }

    /**
     * 导出锁密码信息列表
     */
    @PreAuthorize("@ss.hasPermi('lock:lockPwd:export')")
    @Log(title = "锁密码信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LockPwd lockPwd) {
        List<LockPwd> list = lockPwdService.selectLockPwdList(lockPwd);
        ExcelUtil<LockPwd> util = new ExcelUtil<LockPwd>(LockPwd.class);
        util.exportExcel(response, list, "锁密码信息数据");
    }

    /**
     * 获取锁密码信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('lock:lockPwd:query')")
    @GetMapping(value = "/{pwdId}")
    public AjaxResult getInfo(@PathVariable("pwdId") Long pwdId) {
        return success(lockPwdService.selectLockPwdByPwdId(pwdId));
    }

    /**
     * 新增锁密码信息
     */
    // @PreAuthorize("@ss.hasPermi('lock:lockPwd:add')")
    // @Log(title = "锁密码信息", businessType = BusinessType.INSERT)
    @PostMapping
    @TenantScope
    public AjaxResult add(@RequestBody LockPwd lockPwd) {
        lockPwd.setTenantId(getTenantId());
        lockPwd.setCreateBy(getUsername());
        return toAjax(lockPwdService.insertLockPwd(lockPwd));
    }

    /**
     * 修改锁密码信息
     */
    @PreAuthorize("@ss.hasPermi('lock:lockPwd:edit')")
    @Log(title = "锁密码信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LockPwd lockPwd) {
        return toAjax(lockPwdService.updateLockPwd(lockPwd));
    }

    /**
     * 删除锁密码信息
     */
    @PreAuthorize("@ss.hasPermi('lock:lockPwd:remove')")
    @Log(title = "锁密码信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pwdIds}")
    public AjaxResult remove(@PathVariable Long[] pwdIds) {
        return toAjax(lockPwdService.deleteLockPwdByPwdIds(pwdIds));
    }
}
